package ph.gov.marikina.mcsmtms.tests;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;

import org.apache.commons.codec.binary.Base64;
import org.apache.log4j.Logger;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.ModelAndView;

import ph.gov.marikina.mcsmtms.constants.GlobalConstants.Roads;
import ph.gov.marikina.mcsmtms.constants.GlobalConstants.SituationTypes;
import ph.gov.marikina.mcsmtms.constants.GlobalConstants.TrafficConditionTypes;
import ph.gov.marikina.mcsmtms.domain.MobileUserReport;
import ph.gov.marikina.mcsmtms.rest.webservices.client.BasicSecureSimpleClientHttpRequestFactory;
import ph.gov.marikina.mcsmtms.rest.webservices.client.SimpleCredentials;
import ph.gov.marikina.mcsmtms.rest.webservices.client.SimpleCrendentialsProvider;

import ph.gov.marikina.mcsmtms.rest.webservices.client.RestClient;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;

public class MobileUserReportControllerTest {

	Logger log = Logger.getLogger(MobileUserReportControllerTest.class);

	/** The context. */
	// private static ApplicationContext springContext = new
	// ClassPathXmlApplicationContext(
	// "spring/test-spring-config.xml");

	// private static ApplicationContext applicationContext = new
	// ClassPathXmlApplicationContext(
	// "test-applicationContext.xml");

	private static final String MOBILE_USER_REPORT_REST_SERVICE_URL = "http://localhost:8888/rest/mobileUserReports/";
	//private static final String MOBILE_USER_REPORT_REST_SERVICE_URL = "http://1b.mcsmtms.appspot.com/rest/mobileUserReports/";

	private RestTemplate restTemplate;

	/** The application context. */
	// private static ApplicationContext applicationContext = new
	// ClassPathXmlApplicationContext(
	// "applicationContext.xml");

	@Before
	public void setUp() throws Exception {
		restTemplate = new RestTemplate();
	}

	@Test
	public void listAllMobileUserReportsTest() {

		BasicSecureSimpleClientHttpRequestFactory factory = new BasicSecureSimpleClientHttpRequestFactory();
		factory.setCredentialsProvider(new SimpleCrendentialsProvider(
				new SimpleCredentials("rix:1", "enrico")));
		restTemplate.setRequestFactory(factory);
		String result = restTemplate.getForObject(
				MOBILE_USER_REPORT_REST_SERVICE_URL, String.class);

		// System.out.println(result);

		Gson gson = new Gson();
		JsonElement jelement = new JsonParser().parse(result);
		JsonObject jobject = jelement.getAsJsonObject();
		List<MobileUserReport> mobileUserReportList = gson.fromJson(
				jobject.getAsJsonArray("mobileUserReportList"),
				new TypeToken<List<MobileUserReport>>() {
				}.getType());

		for (MobileUserReport mobileUserReport : mobileUserReportList) {
			System.out.println(mobileUserReport.getId() + " "
					+ mobileUserReport.getRoad());
		}

	}

	@Test
	public void createMobileUserReportTest() {

		BasicSecureSimpleClientHttpRequestFactory factory = new BasicSecureSimpleClientHttpRequestFactory();
		factory.setCredentialsProvider(new SimpleCrendentialsProvider(
				new SimpleCredentials("rix:1", "enrico")));
		// factory.

		Gson gson = new Gson();
		MobileUserReport mobileUserReport = new MobileUserReport();

		mobileUserReport.setRoad(Roads.BAYAN_BAYANAN.getCode());
		mobileUserReport.setSituation(SituationTypes.ACCIDENT.getCode());
		mobileUserReport.setTrafficCondition(TrafficConditionTypes.HEAVY
				.getCode());
		mobileUserReport.setReporter(18);
		mobileUserReport.setReportDateTime(new Date());
		mobileUserReport.setDescription("Test");
		mobileUserReport.setLocationLat(new Double(0.0));
		mobileUserReport.setLocationLng(new Double(0.0));
		mobileUserReport.setSpeed(new Double(0.0));

		HttpHeaders headers = new HttpHeaders();  
        headers.setContentType( MediaType.APPLICATION_JSON );
        
        String jsonData = gson.toJson(mobileUserReport);
        
        System.out.println("jsonData: " + jsonData);
  
        HttpEntity<String> request= new HttpEntity<String>(jsonData, headers);
		
		restTemplate.setRequestFactory(factory);

		String result = restTemplate.postForObject(MOBILE_USER_REPORT_REST_SERVICE_URL, mobileUserReport, String.class);
				// getForObject(SITUATION_REPORT_REST_SERVICE_URL,
//				postForObject(
//				MOBILE_USER_REPORT_REST_SERVICE_URL + "create/", request,
//				String.class);// getForObject(SITUATION_REPORT_REST_SERVICE_URL,
								// String.class);
		
        JsonElement jElement = new JsonParser().parse(result);
        JsonObject jObject = jElement.getAsJsonObject();
        mobileUserReport = gson.fromJson(jObject.get("mobileUserReport"), new TypeToken<MobileUserReport>(){}.getType());
        
        Assert.assertNotNull(mobileUserReport);
        System.out.println(mobileUserReport.getId());
	}
	
	@Ignore
	public void postTest1() {

		BasicSecureSimpleClientHttpRequestFactory factory = new BasicSecureSimpleClientHttpRequestFactory();
		factory.setCredentialsProvider(new SimpleCrendentialsProvider(
				new SimpleCredentials("rix:1", "enrico")));
		// factory.

		Gson gson = new Gson();
		MobileUserReport mobileUserReport = new MobileUserReport();

		mobileUserReport.setRoad(Roads.BAYAN_BAYANAN.getCode());
		mobileUserReport.setSituation(SituationTypes.ACCIDENT.getCode());
		mobileUserReport.setTrafficCondition(TrafficConditionTypes.HEAVY
				.getCode());
		mobileUserReport.setReporter(18);
		mobileUserReport.setReportDateTime(new Date());
		mobileUserReport.setDescription("Test");
		mobileUserReport.setLocationLat(new Double(0.0));
		mobileUserReport.setLocationLng(new Double(0.0));
		mobileUserReport.setSpeed(new Double(0.0));
		
		restTemplate.setRequestFactory(factory);

		String jsonData = gson.toJson(mobileUserReport);
        
        System.out.println("jsonData: " + jsonData);
		
		String result = restTemplate.postForObject(MOBILE_USER_REPORT_REST_SERVICE_URL + "test1/", jsonData, String.class);
		
		System.out.println(result);
		
        JsonElement jElement = new JsonParser().parse(result);
        JsonObject jObject = jElement.getAsJsonObject();
        mobileUserReport = gson.fromJson(jObject.get("mobileUserReport"), new TypeToken<MobileUserReport>(){}.getType());
        
        Assert.assertNotNull(mobileUserReport);
        System.out.println(mobileUserReport.getId());
	}
	
	@Ignore
	public void postTest2() {

		BasicSecureSimpleClientHttpRequestFactory factory = new BasicSecureSimpleClientHttpRequestFactory();
		factory.setCredentialsProvider(new SimpleCrendentialsProvider(
				new SimpleCredentials("rix:1", "enrico")));
		// factory.

		Gson gson = new Gson();
		MobileUserReport mobileUserReport = new MobileUserReport();

		mobileUserReport.setRoad(Roads.BAYAN_BAYANAN.getCode());
		mobileUserReport.setSituation(SituationTypes.ACCIDENT.getCode());
		mobileUserReport.setTrafficCondition(TrafficConditionTypes.HEAVY
				.getCode());
		mobileUserReport.setReporter(18);
		mobileUserReport.setReportDateTime(new Date());
		mobileUserReport.setDescription("Test");
		mobileUserReport.setLocationLat(new Double(0.0));
		mobileUserReport.setLocationLng(new Double(0.0));
		mobileUserReport.setSpeed(new Double(0.0));

		HttpHeaders headers = new HttpHeaders();  
        headers.setContentType( MediaType.APPLICATION_JSON );
        
        String jsonData = gson.toJson(mobileUserReport);
        
        System.out.println("jsonData: " + jsonData);
  
        HttpEntity<String> request= new HttpEntity<String>(jsonData, headers);
		
		restTemplate.setRequestFactory(factory);

		ModelAndView mav = restTemplate.postForObject(MOBILE_USER_REPORT_REST_SERVICE_URL + "test2/", request, ModelAndView.class);
				// getForObject(SITUATION_REPORT_REST_SERVICE_URL,
//				postForObject(
//				MOBILE_USER_REPORT_REST_SERVICE_URL + "create/", request,
//				String.class);// getForObject(SITUATION_REPORT_REST_SERVICE_URL,
								// String.class);
		
//        JsonElement jElement = new JsonParser().parse(result);
//        JsonObject jObject = jElement.getAsJsonObject();
//        mobileUserReport = gson.fromJson(jObject.get("mobileUserReport"), new TypeToken<MobileUserReport>(){}.getType());
//        
//        Assert.assertNotNull(mobileUserReport);
//        System.out.println(mobileUserReport.getId());
	}
	
	@Test
	public void postTest3() throws UnsupportedEncodingException {
		RestClient client = new RestClient(MOBILE_USER_REPORT_REST_SERVICE_URL + "create/");
		
		MobileUserReport mobileUserReport = new MobileUserReport();

		mobileUserReport.setRoad(Roads.BAYAN_BAYANAN.getCode());
		mobileUserReport.setSituation(SituationTypes.ACCIDENT.getCode());
		mobileUserReport.setTrafficCondition(TrafficConditionTypes.HEAVY
				.getCode());
		mobileUserReport.setReporter(1);
		mobileUserReport.setReportDateTime(new Date());
		mobileUserReport.setDescription("Test");
		mobileUserReport.setLocationLat(new Double(0.0));
		mobileUserReport.setLocationLng(new Double(0.0));
		mobileUserReport.setSpeed(new Double(0.0));
		
		Gson gson = new Gson();
		
		String auth = Base64.encodeBase64URLSafeString((("rix:1").getBytes()));
		String mobileUserReportJson = Base64.encodeBase64URLSafeString(gson.toJson(mobileUserReport).getBytes()); 
	    client.addHeader("Authorization", "Basic " + auth);
	    client.addParam("mobileUserReport", mobileUserReportJson);
//	    
//	    //client.add
//	    
		try {
		    client.execute(RestClient.RequestMethod.POST);
		} catch (Exception e) {
		    e.printStackTrace();
		}
//
		String response = client.getResponse();
		
		System.out.println(response);
//		
//		Gson gson = new Gson();
//        JsonElement jElement = new JsonParser().parse(response);
//        JsonObject jObject = jElement.getAsJsonObject();
//        user = gson.fromJson(jObject.get("user"), new TypeToken<User>(){}.getType());
	}
}
